package com.atbeijing.bigdata.spark.core.rdd.operator.transform

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object Spark16_Oper_Transform {

    def main(args: Array[String]): Unit = {

        val conf = new SparkConf().setMaster("local[*]").setAppName("TransformOperator")
        val sc = new SparkContext(conf)

        // TODO 算子 - 转换 - KV - groupByKey (3 / 10)
        val rdd : RDD[(String, Int)] = sc.makeRDD(
            List(
                ("a",1), ("b", 2), ("a", 3)
            )
        )
        // groupByKey算子根据数据的key对数据的value进行分组
        val rdd1: RDD[(String, Iterable[(String, Int)])] = rdd.groupBy(_._1)
        rdd1.collect().foreach(println)
        //(a,CompactBuffer((a,1), (a,3)))
        //(b,CompactBuffer((b,2)))
        val rdd2: RDD[(String, Iterable[Int])] = rdd.groupByKey()
        rdd2.collect().foreach(println)
        //(a,CompactBuffer(1, 3))
        //(b,CompactBuffer(2))
        rdd2.mapValues(_.sum).collect().foreach(println)
        //(a,4)
        //(b,2)



        sc.stop()

    }
    class User {

    }
}
